package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.commons.model.Result;
import com.woniuxy.commons.utils.CommonsUtil;
import com.woniuxy.controller.form.GetAllAccountForm;
import com.woniuxy.controller.form.MyStoreRegisterForm;
import com.woniuxy.dao.model.AccountInfo;
import com.woniuxy.service.AccountInfoService;
import com.woniuxy.service.dto.PageGetAllAccountDto;
import com.woniuxy.service.dto.PageGetAllStoreDto;
import com.woniuxy.controller.form.AccountRegisterForm;
import com.woniuxy.service.AccountInfoService;
import com.woniuxy.service.dto.RegisterDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author psy
 * @since 2022-06-28
 */
@Api(tags = "用户基本信息操作接口，包括登录")
@RestController
public class AccountInfoController {

    @Autowired
    private AccountInfoService accountInfoService;


    /**
     * 未登录状态的返回值
     *
     * @return
     */
    @ApiOperation("登录失败返回界面")
    @GetMapping("/errorlogin")
    public Result errorLogin() {

        return Result.fail(401, "未登录");
    }

    /**
     * 用户注册
     * @param accountRegisterForm
     * @return
     */
    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result accountRegister(@RequestBody AccountRegisterForm accountRegisterForm) {
        RegisterDto registerDto = accountInfoService.accountRegister(accountRegisterForm);
        return Result.success(200, "注册成功", registerDto);
    }

    /**
     * 账号密码登录
     * @param username
     * @param password
     * @return
     */
    @ApiOperation("账号密码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "账号"),
            @ApiImplicitParam(name = "password", value = "密码")
    })
    @PostMapping("/login")
    public Result login(String username, String password) {
        return null;
    }

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @GetMapping("/sendCode/{phone}")
    @ApiOperation("发送验证码")
    @ApiImplicitParam(name = "phone", value = "电话号码")
    public Result sendCode(@PathVariable("phone") String phone) {
        boolean b = accountInfoService.sendCode(phone);
        if (b) {
            return Result.success(200, "发送成功", null);
        }

        return Result.fail(500, "发送失败");
    }

    /**
     * 电话验证码登录
     * @param phone
     * @param code
     * @return
     */
    @PostMapping("/phoneLogin")
    @ApiOperation("电话验证码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "电话号码"),
            @ApiImplicitParam(name = "code", value = "验证码")
    })
    public Result phoneLogin(String phone, String code) {
        return null;
    }

    /**
     * 超级管理员查询所有账号信息的接口
     * @param gaf
     * @return
     */
    @PostMapping("/getAllAccount")
    @ApiOperation("超级管理员查询所有账号信息的接口")
    public Result getAllAccount(@RequestBody GetAllAccountForm gaf) {

        QueryWrapper<AccountInfo> qw = new QueryWrapper<>();

        if (gaf.getKeyword() != null) {
            qw.like("accountName", gaf.getKeyword());
        }

        IPage<AccountInfo> iPage = accountInfoService.page(new Page<AccountInfo>(gaf.getPageNum(), gaf.getPageSize()), qw);
        PageGetAllAccountDto pageGetAllAccountDto = new PageGetAllAccountDto();
        pageGetAllAccountDto.setIndexPage(iPage.getCurrent());
        pageGetAllAccountDto.setTotal(iPage.getTotal());
        pageGetAllAccountDto.setAccounts(iPage.getRecords());

        return Result.success(200, "查询所有账号信息成功", pageGetAllAccountDto);
    }

    /**
     * 商家注册的接口
     * @param myStoreRegisterForm
     * @return
     */
    @PostMapping("/myStoreRegister")
    @ApiOperation("/商家注册的接口")
    public Result storeRegister(@RequestBody MyStoreRegisterForm myStoreRegisterForm) {

        boolean b = accountInfoService.storeAccountRegister(myStoreRegisterForm);

        return Result.success(200, "注册成功", null);
    }


}

